<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    var maxSlidingWindow = function (nums, k) {
      var sw = [];
      var max = [];
      for (var i = 0; i < nums.length; i++) {
        if (i >= k && sw[0] <= i - k) {
          sw.shift(); // 队列移动k个
        }

        while (sw.length && nums[sw[sw.length - 1]] < nums[i]) {
          sw.pop();
        }
        sw.push(i);
        if (i >= k - 1) {
          max.push(nums[sw[0]]);
        }
      }
      return max;
    }
    console.log(maxSlidingWindow(nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3))
  </script>

</body>

</html>